﻿using System;
using System.Runtime.InteropServices;

namespace CSharpHelpers.Timers
{
    /// <summary>
    /// 系统时间帮助类
    /// </summary>
    public class SystemTimeHelper
    {
        public static bool ChangeTime(DateTime newdatetime)
        {
            SystemTime st = new SystemTime();
            st.wYear = (ushort)newdatetime.Year;
            st.wMonth = (ushort)newdatetime.Month;
            st.wDay = (ushort)newdatetime.Day;
            st.wHour = (ushort)newdatetime.Hour;
            st.wMinute = (ushort)newdatetime.Minute;
            st.wSecond = (ushort)newdatetime.Second;
            st.wMiliseconds = (ushort)newdatetime.Millisecond;
            return SystemDateTime.SetLocalTime(ref st);
        }

    }

    class SystemDateTime
    {
        [DllImport("Kernel32.dll")]
        public static extern bool SetLocalTime(ref SystemTime sysTime);

        [DllImport("Kernel32.dll")]
        public static extern void GetLocalTime(ref SystemTime sysTime);
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct SystemTime
    {
        public ushort wYear;
        public ushort wMonth;
        public ushort wDayOfWeek;
        public ushort wDay;
        public ushort wHour;
        public ushort wMinute;
        public ushort wSecond;
        public ushort wMiliseconds;
    }
}
